<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<view>
			<view>
				<view class="header"><image src="../../static/login.png"></image></view>
				<view class="content">
					<view>申请获取以下权限</view>
					<text>获得你的公开信息(昵称，头像、地区等)</text>
				</view>
				<!-- <button class="bottom" type="primary" open-type="getUserInfo" lang="zh_CN" @getuserinfo="wxGetUserInfo" :disabled="logining">授权登录</button> -->
				<button class='bottom' type='primary' withCredentials="true" lang="zh_CN" @click="wxGetUserInfo">
					授权登录
				</button>

			</view>
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
import api from '../../api';
export default {
	data() {
		return {
		};
	},
	onLoad() {
		// this.getUserInfo()
	},
	computed: {
		...mapState(['hasLogin', 'userInfo'])
	},
	methods: {
		...mapMutations(['login']),
		mpLogin(nickName) {
			// this.getUserInfo()
			// return
			let that = this
			uni.login({
				provider: 'weixin',
				success: res => {
					
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							console.log("loginRes.code");
							console.log(res.code);
							
							api.login.login({code:res.code,nickName}).then(res=>{
								if(res.code==200){
									uni.setStorageSync("token",res.data.token)
									uni.setStorageSync("mpId",res.data.mpId)
									uni.reLaunch({
									    url: '../index/index'
									});
								
								}
							}).finally(res=>{
								uni.hideLoading()
							})
							
						}
					});
					
					
				}
			});
		},
		wxGetUserInfo() {
			uni.showLoading({
				title:'登录中'
			})
			// this.$store.commit("userInfoMutations",{})
			// return
						let _this = this;
						// 获取用户信息
						uni.getUserProfile({
							desc: 'weixin',
							success: function(infoRes) {
								console.log(infoRes.userInfo);
								// _this.nickName = infoRes.userInfo.nickName; //昵称
								// _this.avatarUrl = infoRes.userInfo.avatarUrl; //头像
								//页面跳转到登录页面
								_this.$store.commit("userInfoMutations",infoRes.userInfo)
								_this.mpLogin(infoRes.userInfo.nickName)
								// uni.redirectTo({
								// 	url: '../LoginBusiness/LoginBusiness',
								// });
							},
							fail: function(fail) {
								uni.hideLoading()
							}
						});
					},
		getUserInfo(){
			
			// 获取用户信息
			uni.getUserInfo({
				provider: 'weixin',
				success: infoRes => {
					console.log("infoRes");
					console.log(infoRes);
					// uni.switchTab({
					// 	url: '../index/index'
					// })
					
				}
			});
		}
	}
};
</script>

<style>
.header {
	margin: 90rpx 0 90rpx 50rpx;
	border-bottom: 1px solid #ccc;
	text-align: center;
	width: 650rpx;
	height: 300rpx;
	line-height: 450rpx;
}

.header image {
	width: 200rpx;
	height: 200rpx;
}

.content {
	margin-left: 50rpx;
	margin-bottom: 90rpx;
}

.content text {
	display: block;
	color: #9d9d9d;
	margin-top: 40rpx;
}

.bottom {
	border-radius: 80rpx;
	margin: 70rpx 50rpx;
	font-size: 35rpx;
}
</style>
